package com.wang.service.impl;

import com.wang.entity.Cart;
import com.wang.entity.Product;
import com.wang.mapper.CartMapper;
import com.wang.mapper.ProductMapper;
import com.wang.service.CartService;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import com.wang.vo.CartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CartServiceImpl
 * @Description TODO
 * @Date 2024/9/12 14:09
 * @Created by pc
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Resource
    private CartMapper cartMapper;
    @Resource
    private ProductMapper productMapper;
    @Override
    public ResponseUtil findCountByUserId(Integer userId) {
        Integer count = cartMapper.findCountByUserId(userId);
        return ResponseUtil.get(ResponseEnum.CART_FIND_COUNT,count);
    }

    @Override
    public ResponseUtil findCartByUserId(Integer userId) {
        // 这里查询出来的只有部分信息 没有商品信息
        List<Cart> cartList = cartMapper.findCartByUserId(userId);
        // 遍历购物车中的所有商品 然后通过商品的id去查询商品的具体信息
        List<CartVo> cartVoList = new ArrayList<>();
        cartList.forEach(cart -> {
            CartVo cartVo = new CartVo();
            // copyProperties() 将第一个参数对象中的属性赋值给 第二个对象中的同名属性
            BeanUtils.copyProperties(cart,cartVo);
            Integer id = cart.getProductId();
            Product product = productMapper.findById(id);
            cartVo.setName(product.getName());
            cartVo.setPrice(product.getPrice());
            cartVoList.add(cartVo);
        });
        return ResponseUtil.get(ResponseEnum.CART_FIND_CART_INFO,cartVoList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseUtil updateCart(Integer id, Integer quantity, Integer checked) {
        Integer result = cartMapper.updateCart(id, quantity, checked);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_UPDATE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_UPDATE_CART_FAILURE) ;
    }

    @Override
    public ResponseUtil delCartById(Integer id) {
        Integer result = cartMapper.delCartById(id);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE) ;
    }

    @Override
    public ResponseUtil delCartByIds(Integer[] idArr) {
        Integer result = cartMapper.delCartByIds(idArr);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE) ;
    }

    @Override
    public ResponseUtil delCartByUserId(Integer userId) {
        Integer result = cartMapper.delCartByUserId(userId);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE) ;
    }
}
